package com.hxzy.region.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxzy.region.pojo.Region;
import com.hxzy.region.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private RegionService regionService;

    @RequestMapping("/getAll.do")
    @ResponseBody
    public Page<Region> getAll(Integer pageNum, Integer pageSize){
        return regionService.getAll(pageNum,pageSize);
    }

    @RequestMapping(value = "/addRegion.do")
    public String addRegion(String name, String tel, String addr, int no, String email, String remark, @RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response){

        File save_file = new File("D:\\erp_system\\image\\region" + file.getOriginalFilename());
        System.out.println("文件的绝对地址： " + save_file.getAbsolutePath());
        //获取该文件的父文件对象
        save_file.getParentFile().mkdirs();
        //执行保存
        try {
            file.transferTo(save_file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //创建对象
        Region r = new Region();
        r.setTRegionName(name);
        r.setTRegionAddress(addr);
        r.setTRegionEmail(email);
        r.setTRegionImage(save_file.getPath());
        r.setTRegionNo(no);
        r.setTRegionPhone(tel);
        r.setTRegionRemark(remark);
        System.out.println("上传的数据为：" + r);
        //保存场馆信息
        int n = regionService.addRegion(r);
        if(n > 0){
            return "redirect:/view/pages/venue/edit.html";
        }else{
            return "redirect:/view/pages/venue/add.html";
        }
    }

}
